package net.sky.oa.dao.talkpark.impl;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;
import net.sky.oa.dao.SkyBaseDaoImpl;
import net.sky.oa.dao.talkpark.TalAlbumDao;
import net.sky.oa.model.TalAlbum;

@SuppressWarnings("unchecked")
@Repository
public class TalAlbumDaoImpl extends SkyBaseDaoImpl<TalAlbum> implements TalAlbumDao{

	public TalAlbumDaoImpl(){
		super.setPersistType(TalAlbum.class);
	}
	
	public List<TalAlbum> findAllTalAlbum() {
	   String hql="from TalAlbum ta";
		return findByHql(hql);
	}
	
	//根据用户查找对应的相册列表
	public Map  findTalAlbumByUser(final Long userId,final int currentPage,final int lineSize) throws Exception{
		Map map=new HashMap();
		List list=getHibernateTemplate().executeFind(new HibernateCallback(){

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				StringBuffer buffer=new StringBuffer("select new TalAlbum(ta.id,ta.name,ta.picurl,ta.desn) from TalAlbum ta where 1=1 ");
				StringBuffer cbuffer=new StringBuffer("select count(ta.id) from TalAlbum ta where 1=1");
				String hql="";
				if(userId!=-1){
					hql=" and ta.user.id="+userId;
					buffer.append(hql);
				    cbuffer.append(hql);
				}
				cbuffer.append(" and ta.type=1");
				buffer.append(" and ta.type=1 order by ta.id asc");
				List resultList = new ArrayList();
				 Long count = (long)0;
				 Query query = session.createQuery(buffer.toString());
				 if(currentPage!=-1&&lineSize!=-1) {
			     count =(Long)session.createQuery(cbuffer.toString()).uniqueResult();
			     resultList = query.setFirstResult((currentPage - 1) * lineSize)
					.setMaxResults(lineSize).list();
				 }else {
						resultList = query.list();
					}
				    List list = new ArrayList();
					list.add(count);
					list.add(resultList);				
				    return list;
			}
		});
		      map.put("count",list.get(0) );
              map.put("list", list.get(1) );
                return map;
	}
	
	public Map  findCompanyAlbumByUser(final Long userId,final int currentPage,final int lineSize) throws Exception{
		Map map=new HashMap();
		List list=getHibernateTemplate().executeFind(new HibernateCallback(){

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				StringBuffer buffer=new StringBuffer("select new TalAlbum(ta.id,ta.name,ta.picurl,ta.desn) from TalAlbum ta where 1=1 ");
				StringBuffer cbuffer=new StringBuffer("select count(ta.id) from TalAlbum ta where 1=1");
				String hql="";
				if(userId!=-1){
					hql=" and ta.user.id="+userId;
					buffer.append(hql);
				    cbuffer.append(hql);
				}
				buffer.append(" and ta.type=2");
				cbuffer.append(" and ta.type=2");
				buffer.append(" order by ta.id asc");
				List resultList = new ArrayList();
				 Long count = (long)0;
				 Query query = session.createQuery(buffer.toString());
				 if(currentPage!=-1&&lineSize!=-1) {
			     count =(Long)session.createQuery(cbuffer.toString()).uniqueResult();
			     resultList = query.setFirstResult((currentPage - 1) * lineSize)
					.setMaxResults(lineSize).list();
				 }else {
						resultList = query.list();
					}
				    List list = new ArrayList();
					list.add(count);
					list.add(resultList);				
				    return list;
			}
		});
		      map.put("count",list.get(0) );
              map.put("list", list.get(1) );
                return map;
	}
	public Map  findAlbumByEmployees(final Long userId,final int currentPage,final int lineSize) throws Exception{
		Map map=new HashMap();
		List list=getHibernateTemplate().executeFind(new HibernateCallback(){

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				StringBuffer buffer=new StringBuffer("select new TalAlbum(ta.id,ta.name,ta.picurl,ta.desn,ta.user) from TalAlbum ta where 1=1 ");
				StringBuffer cbuffer=new StringBuffer("select count(ta.id) from TalAlbum ta where 1=1");
				String hql="";
				if(userId!=-1){
					hql=" and ta.user.id="+userId;
					buffer.append(hql);
				    cbuffer.append(hql);
				}
				buffer.append(" and ta.type=1 and ta.ispublic=1");
				cbuffer.append(" and ta.type=1 and ta.ispublic=1");
				buffer.append(" order by ta.id asc");
				List resultList = new ArrayList();
				 Long count = (long)0;
				 Query query = session.createQuery(buffer.toString());
				 if(currentPage!=-1&&lineSize!=-1) {
			     count =(Long)session.createQuery(cbuffer.toString()).uniqueResult();
			     resultList = query.setFirstResult((currentPage - 1) * lineSize)
					.setMaxResults(lineSize).list();
				 }else {
						resultList = query.list();
					}
				    List list = new ArrayList();
					list.add(count);
					list.add(resultList);				
				    return list;
			}
		});
		      map.put("count",list.get(0) );
              map.put("list", list.get(1) );
                return map;
	}
	

}
